package com.demo.netty.tomcat;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.*;

import java.nio.charset.StandardCharsets;

public class MyNettyResponse {

    private ChannelHandlerContext cxt;
    private HttpRequest httpRequest;

    public MyNettyResponse(ChannelHandlerContext cxt, HttpRequest httpRequest) {
        this.cxt = cxt;
        this.httpRequest = httpRequest;
    }

    public void write(String out) {
        if (out == null || out.length() == 0) {
            return;
        }
        try {

            DefaultFullHttpResponse resp = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,
                    HttpResponseStatus.OK,
                    Unpooled.wrappedBuffer(out.getBytes(StandardCharsets.UTF_8)));
            resp.headers().set("Content-Type", "text/html;");
            cxt.write(resp);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cxt.flush();
            cxt.close();

        }
    }
}
